﻿using MDK.Common;
using MDK.Model.Models;
using Newtonsoft.Json;
using System;
using System.Windows.Forms;

namespace MDK.Winform
{
    public partial class frmBeltConfig : BaseForm
    {
        private T_BeltSetting _set { get; set; }

        public frmBeltConfig()
        {
            InitializeComponent();
        }

        private void frmBeltConfig_Load(object sender, EventArgs e)
        {
            InitPanel();
            InitBelt();
            BingData();
        }


        /// <summary>
        /// 绑定数据
        /// </summary>
        private void BingData()
        {
            string url = string.Format("BeltSetting/Find?jsonWhere={0}", "");
            _set = WebAPIHelper.Get<T_BeltSetting>(url);

            if (_set == null)
            {
                _set = new T_BeltSetting();
            }
            ckDefault.Checked = _set.Is_Default;
            ckA.Text = _set.A;
            ckB.Text = _set.B;
            ckAB.Text = _set.AB;
            ckO.Text = _set.O;
            ckBelt.Text = _set.Belt;
        }


        /// <summary>
        /// 确认
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_OK_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(_set.Id))
                {
                    _set = new T_BeltSetting();
                    _set.Id = Guid.NewGuid().ToString("N");
                    _set.Is_Default = ckDefault.Checked;
                    _set.A = ckA.Text;
                    _set.B = ckB.Text;
                    _set.AB = ckAB.Text;
                    _set.O = ckO.Text;
                    _set.Belt = ckBelt.Text;
                    WebAPIHelper.Post<T_BeltSetting>("BeltSetting/Add",_set);
                }
                else
                {
                    _set.Is_Default = ckDefault.Checked;
                    _set.A = ckA.Text;
                    _set.B = ckB.Text;
                    _set.AB = ckAB.Text;
                    _set.O = ckO.Text;
                    _set.Belt = ckBelt.Text;
                    WebAPIHelper.Post<T_BeltSetting>("BeltSetting/Modify", _set);
                }
                FastMessageBox.Show("编辑成功");
                DialogResult = DialogResult.OK;
            }
            catch (Exception ex)
            {
                FastMessageBox.Error(ex.Message);
            }
        }

        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
            this.Dispose();
            DialogResult = DialogResult.Cancel;
        }


        /// <summary>
        /// 初始化带条
        /// </summary>
        public void InitPanel()
        {
            for (int i = 1; i <= 20; i++)
            {
                ckA.Properties.Items.Add(i);
                ckB.Properties.Items.Add(i);
                ckAB.Properties.Items.Add(i);
                ckO.Properties.Items.Add(i);
            }
        }

        /// <summary>
        /// 初始化带孔
        /// </summary>
        public void InitBelt()
        {
            for (int i = 1; i <= 32; i++)
            {
                ckBelt.Properties.Items.Add(i);
            }
        }

        /// <summary>
        /// 默认配置改变
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ckDefault_CheckedChanged(object sender, EventArgs e)
        {
            if (ckDefault.Checked)
            {
                ckA.CheckAll();
                ckA.Enabled = false;
                ckB.CheckAll();
                ckB.Enabled = false;
                ckAB.CheckAll();
                ckAB.Enabled = false;
                ckO.CheckAll();
                ckO.Enabled = false;
                ckBelt.CheckAll();
                ckBelt.Enabled = false;
            }
            else
            {
                ckA.Enabled = true;
                ckB.Enabled = true;
                ckAB.Enabled = true;
                ckO.Enabled = true;
                ckBelt.Enabled = true;
            }
        }
    }
}
